package com.ruoyi.system.commons.utils;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.system.domain.dto.ToolJsonResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @Auther 42214
 * @Date 2023/12/21
 * @Description
 */

@Component
public class JsonReader {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 获取工具注册的json文件的内容，并封装成ToolJsonResponseDto
     * @param jsonPath
     * @return ToolJsonResponseDto
     */
    public ToolJsonResponseDto read(String jsonPath) {
        try {
            // 创建FileReader对象，用于读取文件内容
            String json = restTemplate.getForEntity(jsonPath, String.class).getBody();
            ToolJsonResponseDto toolJsonResponseDto = JSON.parseObject(json.toString(), ToolJsonResponseDto.class);
//            System.out.println(toolJsonResponseDto);
            return toolJsonResponseDto;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
